using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;
using net_work.IService.Sys;

namespace net_work.Service.Sys;

public class TeacherService(
    ITeacherRepository teacherRepository,
    IPersonRepository personRepository,
    ICollegeRepository collegeRepository,
    IDepartmentRepository departmentRepository) : ITeacherService
{
    public Task<PagedResult<TeacherResult>> GetTeachersPagedAsync(PagedRequest request)
    {
        return teacherRepository.GetTeacherPagedAsync(request);
    }

    public Task<Teacher?> SelectByIdAsync(long teacherId)
    {
        return teacherRepository.GetByIdAsync(teacherId);
    }

    public async Task<long> InsertTeacher(TeacherAddRequest teacherAddRequest)
    {
        switch (teacherAddRequest.ColleId)
        {
            case <= 0:
                return -1;
            default:
                var college = await collegeRepository.GetByIdAsync(teacherAddRequest.ColleId);
                if (college == null)
                {
                    return -1;
                }

                break;
        }

        switch (teacherAddRequest.DepId)
        {
            case null:
                break;
            case <= 0:
                return -1;
            default:
                var department = await departmentRepository.GetByIdAsync(teacherAddRequest.DepId.Value);
                if (department == null)
                {
                    return -1;
                }

                break;
        }

        switch (teacherAddRequest.PsnId)
        {
            case <= 0:
                return -1;
            default:
                var person = await personRepository.GetByIdAsync(teacherAddRequest.PsnId);
                if (person == null)
                {
                    return -1;
                }

                break;
        }

        return await teacherRepository.InsertAsync(teacherAddRequest);
    }

    public async Task<int> UpdateTeacher(TeacherUpdateRequest teacherUpdateRequest)
    {
        if (teacherUpdateRequest.Id <= 0)
        {
            return -1;
        }

        switch (teacherUpdateRequest.ColleId)
        {
            case <= 0:
                return -1;
            default:
                var college = await collegeRepository.GetByIdAsync(teacherUpdateRequest.ColleId);
                if (college == null)
                {
                    return -1;
                }

                break;
        }

        switch (teacherUpdateRequest.DepId)
        {
            case null:
                break;
            case <= 0:
                return -1;
            default:
                var department = await departmentRepository.GetByIdAsync(teacherUpdateRequest.DepId.Value);
                if (department == null)
                {
                    return -1;
                }

                break;
        }

        switch (teacherUpdateRequest.PsnId)
        {
            case <= 0:
                return -1;
            default:
                var person = await personRepository.GetByIdAsync(teacherUpdateRequest.PsnId);
                if (person == null)
                {
                    return -1;
                }

                break;
        }

        return await teacherRepository.UpdateAsync(teacherUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteTeacherByIds(List<long> ids)
    {
        return await teacherRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }
}